/**
 * Created by z on 2016/11/10.
 */
;(function($,window,document,undefined){

    $('#JQ_create_01').on('click',function() {
        location.href = docApi.url_doc_add_update_html();
    });

    var $standardCode = $('#JQ_standard_code');
    var $standardType = $('#standardType i');
    var $doc_table= $('#JQ_datatable_01')

    /********筛选对象 start**********/
    var filter = {
        getDocFilter: function (data) {
            var filter = {};
            // 分页参数
            if (data) {
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 查询参数
            filter.standardTypes = function () {
                var v=[];
                $standardType.each(function(){
                    var $this = $(this);
                    if($this.hasClass('active')){
                        var data=$this.attr('data-value');
                        v.push(data);
                    }
                });
                return v;
            }();
            filter.standardNumber = function () {
                return $standardCode.val();
            }();
            return filter;
        }
    };
    var $tabl = $('#JQ_datatable_01');

//    加载表格
    var DT01 = $tabl.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getDocFilter(data);
            DyyBase.ajax(docApi.url_dis_list(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            }, {traditional : true});
        },
        'drawCallback': function () {
        },
        'columns': [
            {
                data: 'code', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'name', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'id', render: function (data,type,row) {
                    var fileName = row.code + row.name;
                    return '<i title="下载" class="zy-file-download-icon" data-id="' + data + '" data-name="'+fileName+'"></i>';
            }
            },
            {
                data: 'id', render: function (data) {
                var url =  docApi.url_doc_add_update_html();
                return '<a href="' + url + '?id=' + data + '"><i title="编辑" class="zy-record-add-icon" data-id="' + data + '"></a>';
            }
            },
            {
                data: 'id', render: function (data) {
                return '<i title="删除" class="zy-delete-upload-icon " data-id="' + data + '"></i>';
            }
            }
        ]
    });

   //查询
    $('#JQ_search').click(function () {
        if (DT01) {
            DT01.draw();
        }
    });

    //删除单行
    $tabl.on('click','i.zy-delete-upload-icon', function (event) {
        var $this = $(this);
        var id = $this.attr('data-id');
        $this.MSG({
            type: 'confirm',
            height: 230,
            content: '请确认是否删除该技术标准？',
            yesBtnTxt: '确认',
            yesBtnAct: function () {
                DyyBase.ajax(docApi.url_doc_deleted(), {id: id}, function (dataset) {
                    if (dataset.bool) {
                        $this.MSG({
                            type: 'success',
                            content: dataset.result,
                            yesBtnAct: function () {
                                if (DT01) {
                                    DT01.draw();
                                }
                            }
                        });
                    }
                });
            }
        });
        event.stopPropagation();
        return false;
    });

   // 下载报告
    $doc_table.on('click', 'i.zy-file-download-icon', function(){
        var $this = $(this);
        var $form = $('#JQ_download_form').empty();
        var $iframe = $('#JQ_download_iframe').empty();
        var postObj = {
            businessId: $this.attr('data-id'),
            downFileName: $this.attr('data-name'),
            type: 'docStandard',
            ymd : true
        };
        for(var k in postObj){
            var $input = $('<input type="hidden">');
            $input.prop('name', k).val(postObj[k]);
            $form.append($input);
        }
        $form.submit();
        $iframe.get(0).onload = function(){
            try{
                var content = this.contentWindow.document.body.innerHTML;
                content = $.parseJSON(content);
                if(content.bool){

                }else{
                    $this.MSG({
                        type: 'error',
                        content: content.result
                    });
                }
            }catch(e){
                $this.MSG({
                    type: 'error',
                    content: e.message
                });
            }
        }
    });

})(jQuery,window,document);